package service;

import dao.CroRoleRootDao;
import entity.CroRoleRoot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by xuweijie on 17-5-7.
 */
@Service
public class CroRoleRootService {

    @Autowired
    private CroRoleRootDao croRoleRootDao;

    /**
     * 根据角色id找出相应权限
     * */
    @Transactional(readOnly = true)
    public List<CroRoleRoot> FindRootByRoleId(String roleid){
        List<CroRoleRoot> croRoleRoots=croRoleRootDao.findByProperty("roleId",roleid);
        return croRoleRoots;
    }

    /*
    * 根据角色id删除权限
    * */
    @Transactional(readOnly = false)
    public boolean delRootByRoleId(String roleid){
        String sql="delete from cro_role_root where roleId='"+roleid+"'";
        try {
            croRoleRootDao.executeSql(sql);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 保存角色与权限关系
    * */
    @Transactional(readOnly = false)
    public boolean addRoleRoot(CroRoleRoot croRoleRoot){
        try {
            croRoleRootDao.save(croRoleRoot);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 更新角色与权限信息
    * */
    @Transactional(readOnly = false)
    public boolean updateRoleRoot(CroRoleRoot croRoleRoot){
        try {
            croRoleRootDao.update(croRoleRoot);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
